using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Blazing.BusinessObjects.Cms;
using Blazing.BusinessObjects.Queries;
using Iesi.Collections.Generic;
using System.BusinessObjects.With;
using System.BusinessObjects.Data;

namespace Blazing.WebFacade
{
    [System.ComponentModel.DataObject]
    public class MenuController
    {
        #region Menus
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void DeleteMenu(Menu menu)
        {
            Menu objMenu = Menu.Load(menu.ID);
            objMenu.Delete();
            objMenu.Save(SaveMode.Flush);
            Site.Evict(objMenu.Site.ID);
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public Menu NewMenu(Menu menu)
        {
            menu.Save(SaveMode.Flush);
            Site.Evict(menu.Site.ID);
            return menu;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public Menu UpdateMenu(Menu menu)
        {
            Menu.Evict(menu.ID);
            menu.Save(SaveMode.Flush);
            Site.Evict(menu.Site.ID);
            return menu;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public ISet<Menu> GetMenus(int siteId)
        {
            Site obj = Site.Load(siteId);
            return obj != null ? obj.Menus : null;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public Menu GetMenuByType(int siteId, int menuTypeId)
        {
            return Menu.Fetch(QrySearchMenus.Query(siteId, menuTypeId));            
        }
        #endregion

        #region MenuItems
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public MenuItem DeleteMenuItem(MenuItem menu)
        {
            MenuItem delObj = MenuItem.Load(menu.ID);

            delObj.Delete();
            delObj.Save();
            Menu.Evict(delObj.Menu.ID);
            return delObj;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public MenuItem NewMenuItem(MenuItem menu)
        {
            menu.Save();
            return menu;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public MenuItem UpdateMenuItem(MenuItem menu)
        {
            With.Transaction.Execute(delegate
                 {
                     MenuItem.Evict(menu.ID);
                     menu.Save();
                     Menu.Evict(menu.Menu.ID);
                 }); 
            return menu;
        }

        /// <summary>
        /// Get menu items for a menu
        /// </summary>
        /// <param name="ID">Parent Menu ID</param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public IList<MenuItem> GetMenuItems(int ID)
        {
            return MenuItem.Search(QrySearchMenuItems.Query(ID));
        }

        /// <summary>
        /// Get menu items for a menu, that are not used
        /// </summary>
        /// <param name="ID">Parent Menu ID</param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public IList<MenuItem> GetUnusedMenuItems(int ID)
        {
            IList<MenuItem> itemlist = new List<MenuItem>();

            foreach(MenuItem item in MenuItem.Search(QrySearchMenuItems.Query(ID)))
            {
                //if(item.AttachedPage == null)
                //{
                    itemlist.Add(item);

                //}
            }

            return itemlist;
        }
        #endregion

        #region MenuTypes
        [DataObjectMethodAttribute(DataObjectMethodType.Delete)]
        public void DeleteMenuType(MenuType menu)
        {
            menu.Delete();
            menu.Save();
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Insert)]
        public int NewMenuType(MenuType menu)
        {
            menu.Save();
            return menu.ID;
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Update)]
        public void UpdateMenuType(MenuType menu)
        {
            menu.Save();
        }

        [DataObjectMethodAttribute(DataObjectMethodType.Select)]
        public IList<MenuType> GetMenuTypes()
        {
            return MenuType.Search(QrySearchMenuTypes.Query());
        }
        #endregion
    }
}
